package com.yutel.phone.binh.ui;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;

import com.yutel.phone.binh.R;
import com.yutel.phone.binh.data.BinhConfig;

public class ConfigActivity extends PreferenceActivity implements
		Preference.OnPreferenceChangeListener {
	private EditTextPreference searchHost;
	private EditTextPreference serverHost;
	private CheckBoxPreference imageCache;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.config);
		searchHost = (EditTextPreference) findPreference("searchHost");
		serverHost = (EditTextPreference) findPreference("serverHost");
		imageCache = (CheckBoxPreference) findPreference("imageCache");
		searchHost.setOnPreferenceChangeListener(this);
		serverHost.setOnPreferenceChangeListener(this);
		imageCache.setOnPreferenceChangeListener(this);
		searchHost.setSummary(BinhConfig.searchHost);
		serverHost.setSummary(BinhConfig.serverHost);
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object objValue) {
		if (preference == searchHost) {
			searchHost.setSummary(objValue.toString());
			BinhConfig.setSearchHost(objValue.toString());
		} else if (preference == serverHost) {
			serverHost.setSummary(objValue.toString());
			BinhConfig.setServerHost(objValue.toString());
		} else if(preference == imageCache) {
			BinhConfig.setImageCache(imageCache.isChecked());
		}
		return true;
	}
}